package com.new_wear.project.article.article.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.new_wear.common.exception.BusinessException;
import com.new_wear.common.utils.file.FileUploadUtils;
import com.new_wear.framework.config.NewEraConfig;
import com.new_wear.project.system.dept.domain.Dept;
import com.new_wear.project.system.dept.service.IDeptService;
import com.new_wear.project.system.menu.domain.Menu;
import com.new_wear.project.system.menu.service.IMenuService;
import com.new_wear.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.article.article.domain.CstArticle;
import com.new_wear.project.article.article.service.ICstArticleService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文章Controller
 *
 * @author new
 * @date 2019-12-30
 */
@Controller
@RequestMapping("/article/article")
public class CstArticleController extends BaseController {
    private String prefix = "article/article";

    @Autowired
    private ICstArticleService cstArticleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("article:article:view")
    @GetMapping("/{menuId}")
    public String article(ModelMap mmap, @PathVariable("menuId") Integer menuId) {
        mmap.put("menuId", menuId);
        return prefix + "/article";
    }

    /**
     * 查询文章列表
     */
    @RequiresPermissions("article:article:list")
    @PostMapping("/list/{menuId}")
    @ResponseBody
    public TableDataInfo list(CstArticle cstArticle, @PathVariable("menuId") Integer menuId) {
        startPage();
        cstArticle.setCategory(menuId);
        List<CstArticle> list = cstArticleService.selectCstArticleList(cstArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @RequiresPermissions("article:article:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstArticle cstArticle) {
        List<CstArticle> list = cstArticleService.selectCstArticleList(cstArticle);
        ExcelUtil<CstArticle> util = new ExcelUtil<CstArticle>(CstArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 新增文章
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        Menu menu = menuService.selectMenuById(id);
        mmap.put("menu", menu);
        User user = getSysUser();
        mmap.put("user", user);
        return prefix + "/add";
    }

    /**
     * 新增保存文章
     */
    @RequiresPermissions("article:article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstArticle cstArticle, @RequestParam("file") MultipartFile file, @RequestParam(required = false,value = "multiFile") MultipartFile[] multiFile) throws IOException {
        // 上传文件路径
        String filePath = NewEraConfig.getUploadPath();
        System.out.println("CstArticleController.addSave===="+filePath);
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        System.out.println("CstArticleController.addSave===="+fileName);
        cstArticle.setImg(fileName);
       /* if (multiFile.length>0) {
            String photo = "";
            for (MultipartFile multipartFile : multiFile) {
                String fileName2 = FileUploadUtils.upload(filePath, multipartFile);
                 photo = photo.concat(fileName2 + ",");
            }
            cstArticle.setPhotos(photo.substring(0,photo.length()-1));
        }*/
        return toAjax(cstArticleService.insertCstArticle(cstArticle));
    }

    /**
     * 修改文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        CstArticle cstArticle = cstArticleService.selectCstArticleById(id);
        mmap.put("cstArticle", cstArticle);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章
     */
    @RequiresPermissions("article:article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstArticle cstArticle) {
        return toAjax(cstArticleService.updateCstArticle(cstArticle));
    }

    /**
     * 删除文章
     */
    @RequiresPermissions("article:article:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(cstArticleService.deleteCstArticleByIds(ids));
    }

    /**
     * 修改终审状态
     */
    @GetMapping("/check/{id}")
    public String check(@PathVariable("id") Integer id, ModelMap mmap) {
        CstArticle cstArticle = cstArticleService.selectCstArticleById(id);
        User user = getSysUser();
        mmap.put("cstArticle", cstArticle);
        mmap.put("user", user);
        return prefix + "/check";
    }

    /**
     * 修改保存终审状态
     */
    @RequiresPermissions("article:article:check")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult checkSave(CstArticle cstArticle) {
        if (cstArticle.getIsFinalCheck() == 2) {
            if (cstArticle.getFinalCheckCase() == null || cstArticle.getFinalCheckCase().isEmpty()) {
                throw new BusinessException("不通过的原因不能为空");
            }
            System.out.println("CstArticleController.checkSave=====" + cstArticle.getFinalCheckCase());
        }
        Date date = new Date();
        cstArticle.setFinalCheckTime(date);
        return toAjax(cstArticleService.updateCstArticle(cstArticle));
    }

    /**
     * 取消终审
     */
    @RequiresPermissions("article:article:check")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/cancelFinalCheck")
    @ResponseBody
    public AjaxResult cancelFinalCheck(Integer id) {
        CstArticle cstArticle = cstArticleService.selectCstArticleById(id);
        cstArticle.setIsFinalCheck(0);
        cstArticle.setIsPublish(0);
        cstArticle.setFinalCheckCase(null);
        return toAjax(cstArticleService.updateCstArticle(cstArticle));
    }

    /**
     * 修改发布状态
     */
    @RequiresPermissions("article:article:update")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/publish")
    @ResponseBody
    public AjaxResult publish(Integer id) {
        return toAjax(cstArticleService.publishCstArticle(id));
    }

    /**
     * 新增文章视频地址
     */
    @GetMapping("/addVideo")
    public String addVideo() {

        return prefix + "/edit";
    }

    /**
     * 转移区域
     */
    @GetMapping("/transfer/{id}")
    public String transfer(@PathVariable("id") Integer id, ModelMap mmap) {
        CstArticle cstArticle = cstArticleService.selectCstArticleById(id);
        List<Dept> depts = deptService.selectDeptList(new Dept());
        mmap.put("cstArticle", cstArticle);
        mmap.put("depts", depts);
        return prefix + "/transfer";
    }

    /**
     * 修改保存转移区域
     */
    @RequiresPermissions("article:article:transfer")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/transfer")
    @ResponseBody
    public AjaxResult transferSave(CstArticle cstArticle) {
        return toAjax(cstArticleService.updateCstArticle(cstArticle));
    }
}
